1
|
|
|
/** |
2
|
|
|
* from-bytes: Numbers and strings from bytes. |
3
|
|
|
* Copyright (c) 2017 Rafael da Silva Rocha. |
4
|
|
|
* https://github.com/rochars/byte-data |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
const helpers = require("../src/helpers.js"); |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Turn a byte buffer into what the bytes represent. |
11
|
|
|
* @param {!Array<number>|!Array<string>|Uint8Array} buffer An array of bytes. |
12
|
|
|
* @param {Object} type One of the available types. |
13
|
|
|
* @return {!Array<number>|number|string} |
14
|
|
|
*/ |
15
|
|
|
function fromBytes(buffer, type) { |
16
|
|
|
helpers.fixFloat16Endianness(buffer, type); |
17
|
|
|
helpers.makeBigEndian(buffer, type); |
18
|
|
|
bytesFromBase(buffer, type.base); |
19
|
|
|
let values = readBytes( |
20
|
|
|
buffer, |
21
|
|
|
type |
22
|
|
|
); |
23
|
|
|
if (type.single) { |
24
|
|
|
values = getSingleValue(values, type); |
25
|
|
|
} |
26
|
|
|
return values; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Return the first value from the result value array. |
31
|
|
|
* @param {!Array<number>|string} values The values. |
32
|
|
|
* @param {Object} type One of the available types. |
33
|
|
|
* @return {number|string} |
34
|
|
|
*/ |
35
|
|
|
function getSingleValue(values, type) { |
36
|
|
|
if (type.char) { |
37
|
|
|
values = values.slice(0, type.bits / 8); |
38
|
|
|
} else { |
39
|
|
|
values = values[0]; |
40
|
|
|
} |
41
|
|
|
return values; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Turn a array of bytes into an array of what the bytes should represent. |
46
|
|
|
* @param {!Array<number>|Uint8Array} bytes An array of bytes. |
47
|
|
|
* @param {Object} type The type. |
48
|
|
|
* @return {!Array<number>|string} |
49
|
|
|
*/ |
50
|
|
|
function readBytes(bytes, type) { |
51
|
|
|
let values = []; |
52
|
|
|
let i = 0; |
53
|
|
|
let len = bytes.length - (type.offset - 1); |
54
|
|
|
while (i < len) { |
55
|
|
|
values.push( |
56
|
|
|
type.sign(type.reader(bytes, i, type)) |
57
|
|
|
); |
58
|
|
|
i += type.offset; |
59
|
|
|
} |
60
|
|
|
if (type.char) { |
61
|
|
|
values = values.join(""); |
62
|
|
|
} |
63
|
|
|
return values; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Turn bytes to base 10. |
68
|
|
|
* @param {!Array<number>|Uint8Array} bytes The bytes as binary or hex strings. |
69
|
|
|
* @param {number} base The base. |
70
|
|
|
*/ |
71
|
|
|
function bytesFromBase(bytes, base) { |
72
|
|
|
if (base != 10) { |
73
|
|
|
let i = 0; |
74
|
|
|
let len = bytes.length; |
75
|
|
|
while(i < len) { |
76
|
|
|
bytes[i] = parseInt(bytes[i], base); |
77
|
|
|
i++; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Turn numbers and strings to bytes. |
84
|
|
|
* @param {!Array<number>|number|string} values The data. |
85
|
|
|
* @param {Object} type One of the available types. |
86
|
|
|
* @return {!Array<number>|!Array<string>|Uint8Array} the data as a byte buffer. |
87
|
|
|
*/ |
88
|
|
|
function toBytes(values, type) { |
89
|
|
|
let bytes = writeBytes(values, type); |
90
|
|
|
helpers.makeBigEndian(bytes, type); |
91
|
|
|
helpers.outputToBase(bytes, type.bits, type.base); |
92
|
|
|
helpers.fixFloat16Endianness(bytes, type); |
93
|
|
|
return bytes; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Write values as bytes. |
98
|
|
|
* @param {!Array<number>|number|string} values The data. |
99
|
|
|
* @param {Object} type One of the available types. |
100
|
|
|
* @return {!Array<number>} the bytes. |
101
|
|
|
*/ |
102
|
|
|
function writeBytes(values, type) { |
103
|
|
|
let i = 0; |
104
|
|
|
let j = 0; |
105
|
|
|
let len = values.length; |
106
|
|
|
let bytes = []; |
107
|
|
|
while (i < len) { |
108
|
|
|
j = type.writer(bytes, type.overflow(values[i]), j); |
109
|
|
|
i++; |
110
|
|
|
} |
111
|
|
|
return bytes; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
module.exports.toBytes = toBytes; |
115
|
|
|
module.exports.fromBytes = fromBytes; |
116
|
|
|
|